<template>
 <!--  <div class="tools" :class="theme">
    <span>主题</span>
    <select v-model="theme" @change="onSwitchTheme">
      <option value="vs-dark">vs-dark</option>
      <option value="vs">vs</option>
      <option value="hc-black">hc-black</option>
    </select>
    <span>语言</span>
    <select @change="onSwitchLanguage">
      <option>json</option>
      <option>sql</option>
      <option>yaml</option>
      <option>css</option>
      <option>javascript</option>
    </select>
    <button @click="onFormat">格式化</button>
    <button>拷贝</button>
  </div> -->
  <textarea ref="codeEditBox" class="codeEditBox"></textarea>
</template>
<script lang="ts">
  import { defineComponent, onBeforeUnmount, onMounted, ref, watch } from 'vue';

  export default defineComponent({
    components: {},
    props: {
      value: {
        type: String,
        default: '',
      },
      language: {
        type: String,
        default: 'json',
      },
    },
    emits: ['update:modelValue', 'change', 'editor-mounted'],
    setup(props, { emit }) {
      console.log(props);
      // (self as any).MonacoEnvironment = {
      //   getWorker(_: string, label: string) {
      //     if (label === "json") {
      //       return new JsonWorker();
      //     }
      //     if (["css", "scss", "less"].includes(label)) {
      //       return new CssWorker();
      //     }
      //     if (["html", "handlebars", "razor"].includes(label)) {
      //       return new HtmlWorker();
      //     }
      //     if (["typescript", "javascript"].includes(label)) {
      //       return new TsWorker();
      //     }
      //     return new EditorWorker();
      //   }
      // };
      let editor: any;
      const codeEditBox = ref();
      const language = ref(props.language || 'json');
      const theme = ref('hc-black');

      watch(
        () => props.value,
        (newValue) => {
          if (editor) {
            const value = editor.getValue();
            if (newValue !== value) {
              editor.setValue(newValue);
            }
            onFormat();
          }
        }
      );

      const onFormat = () => {
        editor.getAction('editor.action.formatDocument').run(); //自动格式化代码
      };
      watch(
        () => props.language,
        (newValue) => {
          language.value = newValue;
        }
      );

      onBeforeUnmount(() => {
        editor.dispose();
      });

      onMounted(() => {});

      // eslint-disable-next-line vue/no-dupe-keys
      return { codeEditBox, language, theme, onFormat };
    },
  });
</script>
<style lang="less" scoped>
  .codeEditBox {
    width: 100%;
    flex: 1;
    min-height: 100px;
    height: 100%;
    overflow-y: auto;
  }

  .codeEditBox1 {
    height: calc(100% - 323px);
  }

  .tools {
    height: 20px;
    display: block;

    span {
      padding: 0 10px;
    }

    button {
      padding: 0 10px;
    }

    select {
      width: 120px;
      outline: none;
    }
  }

  .tools.vs-dark {
    background: #1f1f1f;
    border: none;
    border-bottom: 1px solid #323232;
    color: #cccccc;

    select {
      background: #1f1f1f;
    }

    button:hover {
      color: white;
    }
  }
</style>
